<template>
  <el-dialog :visible.sync="visible" width="50%" custom-class="default-dialog" :close-on-click-modal="false"
    @close="cancel" append-to-body v-dialogDrags>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="120px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="断面名称" prop="ecologicalSectionId">
            <el-select size="small" v-model="form.ecologicalSectionId" filterable placeholder="请选择">
              <el-option
                v-for="item in dmOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="监测类型" prop="dmlx">
            <el-select size="small" v-model="form.dmlx"  placeholder="请选择" @change="onZBSelectChange">
              <el-option
                v-for="item in jclxOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="监测值" prop="monitorValue">
            <el-input  v-model="form.monitorValue" placeholder="请输入监测值"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="监测日期" prop="date">
            <el-date-picker
              v-model="form.date"
              type="month"
              format="yyyy-MM"
              value-format="yyyy-MM"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      title: '',
      form: {
        dictName: '',
        date:'',
        hehuType: [],
      },
      jclxOptions: [
        {
          label: '水位',
          value: '1'
        }, {
          label: '流量',
          value: '2'
        }],
      dmOptions:[],
      rules: {
        ecologicalSectionId: [{ required: true, message: '请输入断面名称', trigger: 'blur' }],
        dmlx: [{ required: true, message: '请输入断面类型', trigger: 'blur' }],
        monitorValue: [{ required: true, message: '请输入监测值', trigger: 'blur' }],
        date: [{ required: true, message: '请输入日期', trigger: 'blur' }]
      },
      type: '',
      dmtype:'',
    };
  },
  mounted() {
    this.getparams();
  },
  methods: {
    onZBSelectChange(){
      this.getDMMC()
    },
    async getDMMC(){
      let res = await this.$api.getHasSectionMonitorName(this.form.dmlx,{ keywords:''})
      if(res && res.code == 200){
        this.dmOptions = res.data || []
        this.form.ecologicalSectionId = this.dmOptions[0].id;
      }
    },
    getparams(){
      this.$api.getSectionMonitorName({ keywords:""}).then((res) => {
        if(res && res.code == 200){
          this.dmOptions = res.data || []
        }
      })
    },
    showModal(type,dmtype, row) {
      this.visible = true
      this.type = type
      this.dmtype=dmtype

      if (type == 'add') {
        this.title = '新增'
        this.form = {
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        // this.form.dmlx = '1';
        this.getInfoInit(row.id)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.id)
      }
      this.form.dmlx=dmtype
    },
    // 详情
    getInfoInit(id) {
      this.$api.getSectionMonitor(this.dmtype,{ id: id }).then(res => {
        if (res && res.code == 200) {
          let data = res.data || {}
          this.form = data
          if(this.dmtype==='1'){
            this.form.monitorValue=  Number(data.waterLevel)
          }else{
            this.form.monitorValue= Number(data.flow)
          }
          this.form.dmlx = this.dmtype
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.type == 'add') {
            this.$api.addSectionMonitor(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          } else if (this.type == 'edit') {
            this.$api.updateSectionMonitor(this.form).then(res => {
              if (res && res.code == 200) {
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
  }
};
</script>
<style lang="less" scoped>
.el-form-item__label {
  width: 150px;
}
</style>
